package com.ymx.darling.dialect;

import com.ymx.darling.bean.sql.BoundSql;
import com.ymx.darling.bean.sql.SqlStatement;

import java.util.List;

/**
 * @author 爱Java的小于
 */
public class DmDialect implements Dialect {

    private String splitSql(String sql) {
        // 达梦数据库分页语法
        return sql + " LIMIT ? OFFSET ?";
    }

    @Override
    public String getDataBaseType() {
        return "dm";
    }

    @Override
    public void paginate(BoundSql boundSql, int pageNum, int pageSize) {
        List<Object> rightParameters = boundSql.getRightParameters();
        int offset = (pageNum - 1) * pageSize;
        rightParameters.add(pageSize);
        rightParameters.add(offset);

        String pageSql = this.splitSql(boundSql.getSql());
        boundSql.setSql(pageSql);
    }

    @Override
    public void limit(SqlStatement statement, long limit, long offset) {
        List<Object> paramList = statement.getParamList();
        paramList.add(limit);
        paramList.add(offset);
        statement.setSql(this.splitSql(statement.getSql()));
    }

    @Override
    public String charLength(String column) {
        return "LENGTH("+column+")";
    }
}
